package com.mcxx.modules.miniSalvation.family.service.write;

import com.mcxx.common.exception.BizException;
import com.mcxx.convert.FamilyConvert;
import com.mcxx.modules.miniSalvation.family.dao.read.FamilyMainReadDao;
import com.mcxx.modules.miniSalvation.family.dao.read.FamilyMemberReadDao;
import com.mcxx.modules.miniSalvation.family.dao.read.FamilySpecialReadDao;
import com.mcxx.modules.miniSalvation.family.dao.read.FamilySupportReadDAO;
import com.mcxx.modules.miniSalvation.family.dao.write.FamilyMainHistoryWriteDao;
import com.mcxx.modules.miniSalvation.family.dao.write.FamilyMemberHistoryWriteDAO;
import com.mcxx.modules.miniSalvation.family.dao.write.FamilySpecialHistoryWriteDao;
import com.mcxx.modules.miniSalvation.family.dao.write.FamilySupportHistoryWriteDao;
import com.mcxx.modules.miniSalvation.family.entity.*;
import com.mcxx.modules.miniSalvation.family.service.read.FamilyMainHistoryReadService;
import com.mcxx.modules.miniSalvation.order.dao.read.OrderReadDao;
import com.mcxx.modules.miniSalvation.order.entity.OrderEntity;
import com.mcxx.modules.miniSalvation.salary.service.read.FamilySalaryReadService;
import com.mcxx.util.CommonUtil;
import com.mcxx.util.Constant;
import com.mcxx.util.FailureCode;
import com.yinhai.ta404.core.security.vo.UserAccountVo;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
@TaTransactional
public class FamilyMainHistoryWriteService {

    @Autowired
    private FamilyMainHistoryWriteDao familyMainHistoryWriteDao;
    @Autowired
    private FamilyMainReadDao familyMainReadDao;
    @Autowired
    private FamilyMemberReadDao familyMemberReadDao;
    @Autowired
    private FamilyMainWriteService familyMainWriteService;
    @Autowired
    private OrderReadDao orderReadDao;
    @Autowired
    private FamilyMemberHistoryWriteDAO familyMemberHistoryWriteDAO;

    @Autowired
    private FamilySpecialReadDao familySpecialReadDao;

    @Autowired
    private FamilySupportReadDAO familySupportReadDAO;
    @Autowired
    private FamilySupportHistoryWriteDao familySupportHistoryWriteDao;
    @Autowired
    private FamilySpecialHistoryWriteDao familySpecialHistoryWriteDao;
    @Autowired
    private FamilySalaryReadService familySalaryReadService;
    @Autowired
    private FamilyMainHistoryReadService familyMainHistoryReadService;


    public int update(FamilyMainHistoryEntity familyMainHistory) {
    	return familyMainHistoryWriteDao.update(familyMainHistory);
    }


    public int updateIgnoreNull(FamilyMainHistoryEntity familyMainHistory) {
    	return familyMainHistoryWriteDao.updateIgnoreNull(familyMainHistory);
    }

    public int insertIgnoreNull(FamilyMainHistoryEntity familyMainHistory){
        return familyMainHistoryWriteDao.insertIgnoreNull(familyMainHistory);
    }


    public int delete(FamilyMainHistoryEntity familyMainHistory) {
    	return familyMainHistoryWriteDao.delete(familyMainHistory);
    }


    public int backHistory(String orderId, String familyId, String rescueYears, String salvationNo,UserAccountVo userAccountVo){

        OrderEntity orderEntity = orderReadDao.getOrderById(orderId);
        FamilyMainEntity familyMainEntity = familyMainReadDao.getById(familyId);
        List<FamilyMemberEntity>  familyMemberEntityList = familyMemberReadDao.queryByFamilyId(familyId);

        List<FamilySpecialEntity> familySpecialEntities = familySpecialReadDao.querFamilyspecialByFamilyId(familyId);
        List<FamilySupportEntity> familySupportEntities = familySupportReadDAO.queryFamilySupportByFamilyId(familyId);

        if(CollectionUtils.isNotEmpty(familySpecialEntities)){

            for(FamilySpecialEntity familySpecialEntity:familySpecialEntities){
                FamilySpecialHistoryEntity familySpecialHistoryEntity = FamilyConvert.convertFamilySpecialHistoryEntity(orderEntity, familySpecialEntity);
                familySpecialHistoryWriteDao.insertIgnoreNull(familySpecialHistoryEntity);

            }
        }

        if(CollectionUtils.isNotEmpty(familySupportEntities)){

            for(FamilySupportEntity familySupportEntity:familySupportEntities){
                FamilySupportHistoryEntity familySupportHistoryEntity = FamilyConvert.convertFamilySupportHistoryEntity(orderEntity, familySupportEntity);
                familySupportHistoryWriteDao.insertFamilySupportHistoryFatch(familySupportHistoryEntity);
            }
        }



        String masterState = "01";

        for(FamilyMemberEntity familyMemberEntity : familyMemberEntityList){

            FamilyMemberHistoryEntity familyMemberHistoryEntity = FamilyConvert.convertFamilyMemberHistoryEntity(orderEntity, familyMemberEntity, familyMainEntity, userAccountVo);
            if(Constant.DomesticRelation.ONESELF.equals(familyMemberEntity.getFamilyRelation())){
                masterState =  familyMemberEntity.getState();
            }

            familyMemberHistoryWriteDAO.insertIgnoreNull(familyMemberHistoryEntity);
        }





        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date rescueDate = null;
        try {
            rescueDate = sdf.parse(rescueYears + "-01");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(rescueDate);
        rightNow.add(Calendar.MONTH, -1);
        Date dt1 = rightNow.getTime();
        String reStr = sdf.format(dt1);

        if (!Constant.OrderType.NEW.equals(orderEntity.getType())){


            String endDate = familySalaryReadService.getFamilySalaryMaxDate(orderId,familyId,orderEntity.getBusinessType());
            if(!StringUtils.isEmpty(endDate)){
                if(endDate.compareTo(reStr) > 0){
                    FamilyMainHistoryEntity familyMainHistoryEntity = familyMainHistoryReadService.getByOrderId(orderEntity.getSourOrderId());

                    if(!StringUtils.isEmpty(familyMainHistoryEntity.getFristStartDate())){
                        rescueYears = familyMainHistoryEntity.getFristStartDate();
                    }
                }
            }

        }






        familyMainEntity.setPeopleState(masterState);
        FamilyMainHistoryEntity familyMainHistoryEntity = FamilyConvert.convertFamilyMainHistoryEntity(orderEntity, familyMainEntity, rescueYears, salvationNo, userAccountVo);

        int rst = familyMainHistoryWriteDao.insertIgnoreNull(familyMainHistoryEntity);
        if(StringUtils.isNotBlank(salvationNo)){
            FamilyMainEntity updateFamily = new FamilyMainEntity();
            updateFamily.setId(familyId);
            updateFamily.setSalvationNo(salvationNo);
            familyMainWriteService.updateFamily(updateFamily);
        }
        if(rst <= 0){
            throw new BizException("-1", "操作失败");
        }
        return 1;
    }


    public void batchDeleteByOrderId(List<String> orderIdList) {
        int rows = familyMainHistoryWriteDao.batchDeleteByOrderId(orderIdList);
        if (rows < 1) {
            throw new BizException(FailureCode.ERR_9999);
        }
    }



    public int copyHistoryRecordByOrderId(String oldOrderId, String newOrderId){
        return familyMainHistoryWriteDao.copyHistoryRecordByOrderId(oldOrderId, newOrderId);
    }

    public int updateMemberName(String name,String orderId,String familyId) {
        return familyMainHistoryWriteDao.updateMemberName(name,orderId,familyId);
    }


    public void updateLowerStatus(String familyId,String status) {
        familyMainHistoryWriteDao.updateLowerStatus(familyId,status);
    }
}
